//
//  DeleteGroupViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/19.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "ModifyGroupInfoViewController.h"

@interface ModifyGroupInfoViewController ()<UITextViewDelegate>
/**
 title
 */
@property (nonatomic , strong) UILabel *titleLab;
/**
 textFieldBgView
 */
@property (nonatomic , strong) UIView *textFieldBgView;
/**
 textField
 */
@property (nonatomic , strong) UITextField *textField;
/**
 textBgView
 */
@property (nonatomic , strong) UIView *textBgView;
/**
 群公告
 */
@property (nonatomic , strong) UITextView *textView;
@end

@implementation ModifyGroupInfoViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    switch (self.modifyGroupInfoType) {
        case ModifyGroupInfoTypeGroupName:
        {
            self.title = @"修改群名称";
        }
            break;
        case ModifyGroupInfoTypeMyName:
        {
            self.title = @"我在本群昵称";

        }
            break;
        case ModifyGroupInfoTypeNotice:
        {
            self.title = @"群公告";

        }
            break;
        default:
            break;
    }
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 26)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"#656565"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
    self.navigationItem.leftBarButtonItem = cancelItem;
    
    UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 56, 26)];
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    saveButton.backgroundColor = [UIColor primaryColor];
    saveButton.layer.cornerRadius = 5;
    saveButton.layer.masksToBounds = YES;
    [saveButton addTarget:self action:@selector(saveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithCustomView:saveButton];
    self.navigationItem.rightBarButtonItem = saveItem;
    
}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.titleLab];
    [self.view addSubview:self.textFieldBgView];
    [self.textFieldBgView addSubview:self.textField];
    
    [self.view addSubview:self.textBgView];
    [self.textBgView addSubview:self.textView];
}

- (void) initLayout {
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(15);
        make.height.mas_equalTo(18);
        make.right.mas_equalTo(-10);
    }];
    [self.textFieldBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.titleLab.mas_bottom).offset(5);
        make.height.mas_equalTo(56);
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    [self.textBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(10);
    }];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-15);
    }];
}

- (void) initAction {
    
}

- (void)initData {
    switch (self.modifyGroupInfoType) {
        case ModifyGroupInfoTypeGroupName:
        {
            self.titleLab.text = @"群聊名称";
            self.textField.placeholder = @"请输入群名称";
            self.titleLab.hidden = NO;
            self.textFieldBgView.hidden = NO;
        }
            break;
        case ModifyGroupInfoTypeMyName:
        {
            self.titleLab.text = @"内容在这里可以设置在本群昵称，这个昵称只显示在该群。";
            self.textField.placeholder = @"请输入昵称";
            self.titleLab.hidden = NO;
            self.textFieldBgView.hidden = NO;
        }
            break;
        case ModifyGroupInfoTypeNotice:
        {
            self.textBgView.hidden = NO;
            self.textView.text = self.groupNotice;
        }
            break;
        default:
            break;
    }
}

#pragma mark - delegate
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length > 70) {
        textView.text = [textView.text substringToIndex:70];
        [self toastWithContent:@"限制字数为70字数"];
    }
}
#pragma mark - target action
- (void)cancelButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)saveButtonClick {
    
    NSString *modifyText = self.textField.text;
    NSString *groupNotice = self.textView.text;
    switch (self.modifyGroupInfoType) {
        case ModifyGroupInfoTypeGroupName:
        {
            if ([JTool iSNull:modifyText]) {
                [self toastWithContent:@"请输入修改名称"];
                return;
            }
            [self modifyGroupNameWithText:modifyText];
        }
            break;
        case ModifyGroupInfoTypeMyName:
            
        {
            if ([JTool iSNull:modifyText]) {
                [self toastWithContent:@"请输入修改名称"];
                return;
            }
            [self modifyMyNameInGroupWithText:modifyText];
            
        }
            break;
        case ModifyGroupInfoTypeNotice:
        {
            if ([JTool iSNull:groupNotice]) {
                [self toastWithContent:@"请输入群公告"];
                return;
            }
            [self modifyGroupNoticeWithText:groupNotice];
            
        }
            break;
        default:
            break;
    }
    
}
#pragma mark - private method
//修改群名称
- (void)modifyGroupNameWithText:(NSString *)text {
    @weakify(self);
    [[TIMGroupManager sharedInstance] modifyGroupName:self.groupId groupName:text succ:^{
        @strongify(self);
        
        [self.navigationController popViewControllerAnimated:YES];
    } fail:^(int code, NSString *msg) {
        @strongify(self);
        [self toastWithContent:msg];
    }];
}
//修改我在群中 的昵称
- (void)modifyMyNameInGroupWithText:(NSString *)text {
    NSString *user = [TIMManager sharedInstance].getLoginUser;
    @weakify(self);
    [[TIMGroupManager sharedInstance] modifyGroupMemberInfoSetNameCard:self.groupId user:user nameCard:text succ:^{
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    } fail:^(int code, NSString *msg) {
        @strongify(self);
        [self toastWithContent:msg];
    }];
}
//修改群公告
- (void)modifyGroupNoticeWithText:(NSString *)text {
    
    @weakify(self);
    [[TIMGroupManager sharedInstance] modifyGroupNotification:self.groupId notification:text succ:^{
        
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    } fail:^(int code, NSString *msg) {
        @strongify(self);
        [self toastWithContent:msg];
    }];
}
#pragma mark - getter and setter
- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.textColor = [UIColor colorWithHexString:@"#B0B0B0"];
        _titleLab.font = [UIFont systemFontOfSize:13];
        _titleLab.hidden = YES;
    }
    return _titleLab;
}
- (UIView *)textFieldBgView {
    if (!_textFieldBgView) {
        _textFieldBgView = [[UIView alloc] init];
        _textFieldBgView.backgroundColor = [UIColor whiteColor];
        _textFieldBgView.hidden = YES;
    }
    return _textFieldBgView;
}
- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        [_textField setValue:[UIFont systemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
        [_textField setValue:[UIColor colorWithHexString:@"#B0B0B0"] forKeyPath:@"_placeholderLabel.textColor"];
    }
    return _textField;
}
- (UIView *)textBgView {
    if (!_textBgView) {
        _textBgView = [[UIView alloc] init];
        _textBgView.backgroundColor = [UIColor whiteColor];
        _textBgView.hidden = YES;
    }
    return _textBgView;
}
- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.delegate = self;
    }
    return _textView;
}
@end
